package org.zjut.db;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.zjut.pso.IPSO;
import org.zjut.pso.PSOBase;
import org.zjut.pso.PSOone;
import org.zjut.util.CommonUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Rising
 * @date 2017/9/20
 * @des 知识点推荐/资源推荐
 */
public class RcommondDataBase extends JdbcDaoSupport {
    @Autowired
    LearnConceptDataBase mLearnConceptDataBase;
    @Autowired
    ResourceDataBase mResourceDataBase;
    @Autowired
    PSOBase psoBase;

    String dataBase = "pso_recommond";

    /**
     * 获取系统为用户推荐的知识点
     * @param params
     * @return
     */
    public Map<String,Object> getRecommonKnowledge(Map<String,Object> params){
        String userCode = null , courseCode = null;
        Map<String,Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
            courseCode = params.get("courseCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }

        String sql = "SELECT a.*  FROM "+dataBase+".view_recommond_knowledge as a where a.userCode = ? and a.courseCode = ? and a.isFinish = 0"; //没有完成学习的知识点

        try {
            List<Map<String,Object>> knoledges = getJdbcTemplate().queryForList(sql,new Object[]{userCode,courseCode});
            Map<String,Object> message = new HashMap<>();
            message.put("conceptInfos",knoledges);
            result.put("success",true);
            result.put("message",message);

        }catch (Exception e){
            System.out.println(e.getMessage()+" ");
            return CommonUtil.setErrorInfo("查询失败");
        }

        return result;
    }

    /**
     * 获取系统为用户推荐的资源
     * @param params
     * @return
     */
    public Map<String,Object> recommondResource(Map<String,Object> params){
        String userCode = null;
        Map<String,Object> result = new HashMap<>();
        try {
            userCode = params.get("userCode").toString();
        }catch (Exception e){
            return CommonUtil.setErrorInfo("请检查参数");
        }
        String sql = "SELECT a.* FROM "+dataBase+".view_recommond_resource as a " +
                "left join learner_course as b on a.userCode  = b.learnerCode " +
                "where b.status = 1 and a.userCode  = ? "; //没完成学习的

        try {
            List<Map<String,Object>> resources = getJdbcTemplate().queryForList(sql,new Object[]{userCode});
            Map<String,Object> message = new HashMap<>();
            message.put("resourceInfos",resources);
            result.put("success",true);
            result.put("message",message);
        }catch (Exception e){
            System.out.println(e.getMessage()+" ");
            return CommonUtil.setErrorInfo("查询失败");
        }
        return result;
    }


    /**
     * 完成某个资源的学习
     */
    public void finishResource(String userCode , String resourceCode){
        String sql = "UPDATE "+dataBase+".learner_recommon_resource SET status = 2 where userCode = ? and  knowledgeCode = ? ";
        try {
            getJdbcTemplate().update(sql,new Object[]{userCode,resourceCode});
        }catch (Exception e){
            System.out.println("finishResource"+e.getMessage());
            return ;
        }

    }

    /**
     * 结束某门课程的学习
     * 同时结束该课程下面所有的知识点的学习
     * @param userCode
     * @param courseCode
     */
    public void finishCourse(String userCode,String courseCode){
        String sql = "UPDATE "+dataBase+".learner_course SET status = 2  where learnerCode = ? and courseCode = ? ";
        try {
            getJdbcTemplate().update(sql,new Object[]{userCode,courseCode});
        }catch (Exception e){
            System.out.println("finishCourse:"+e.getMessage());
        }
    }


}
